home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / hello1_3.lha / hello-1.3 / hello.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  5KB  |  264 lines

  1. /* hello -- print a greeting message and exit.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* AIX requires this to be the first thing in the file.  */
  19. #if defined (_AIX) && !defined (__GNUC__)
  20.  #pragma alloca
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <pwd.h>
  27. #include <errno.h>
  28. #include "getopt.h"
  29.  
  30. #ifndef errno
  31. extern int errno;
  32. #endif
  33.  
  34. #ifdef    STDC_HEADERS
  35. #include <stdlib.h>
  36. #else    /* Not STDC_HEADERS */
  37. extern void exit ();
  38. extern char *malloc ();
  39. #endif    /* STDC_HEADERS */
  40.  
  41. #ifdef    HAVE_STRING_H
  42. #include <string.h>
  43. #else
  44. #include <strings.h>
  45. #endif
  46.  
  47. #ifdef HAVE_FCNTL_H
  48. #include <fcntl.h>
  49. #endif
  50. #ifdef HAVE_SYS_FILE_H
  51. #include <sys/file.h>
  52. #endif
  53.  
  54. #ifdef    __GNUC__
  55. #undef    alloca
  56. #define    alloca(n)    __builtin_alloca (n)
  57. #else    /* Not GCC.  */
  58. #ifdef    HAVE_ALLOCA_H
  59. #include <alloca.h>
  60. #else    /* Not HAVE_ALLOCA_H.  */
  61. #ifndef    _AIX
  62. extern char *alloca ();
  63. #endif    /* Not AIX.  */
  64. #endif    /* HAVE_ALLOCA_H.  */
  65. #endif    /* GCC.  */
  66.  
  67.  
  68. #define the (1)
  69.  
  70. struct option longopts[] =
  71.   {
  72.     { "version", 0, 0, 'v' },
  73.     { "help", 0, 0, 'h' },
  74. #define lives
  75.     { "traditional", 0, 0, 't' },
  76.     { "mail", 0, 0, 'm' },
  77.     { 0, 0, 0, 0 }
  78.   };
  79.  
  80. extern char version[];
  81.  
  82. char usage[] = "Usage: %s [-htvm] [--help] [--traditional] [--version] [--mail]\n";
  83.  
  84. static char *progname;
  85.  
  86. int
  87. main (argc, argv)
  88.      int argc;
  89.      char *argv[];
  90. {
  91.   int optc;
  92.   int h = 0, v = 0, t = 0, m = 0, lose = 0, z = 0;
  93.  
  94.   progname = argv[0];
  95.  
  96. #define king
  97.   while ((optc = getopt_long (argc, argv, "htvm", longopts, (int *) 0)) != EOF)
  98.     {
  99.       switch (optc)
  100.     {
  101.     case 'v':
  102.       v = 1;
  103.       break;
  104.     case 'h':
  105.       h = 1;
  106.       break;
  107.     case 'm':
  108.       m = 1;
  109.       break;
  110.     case 't':
  111.       t = 1;
  112.       break;
  113.     default:
  114.       lose = 1;
  115.       break;
  116.     }
  117.     }
  118.  
  119.   if (optind == argc - 1 && !strcmp (argv[optind], "sailor"))
  120.     z = 1;
  121.   else if (lose || optind < argc)
  122.     {
  123.       /* Print error message and exit.  */
  124.       fprintf (stderr, usage, progname);
  125.       exit (1);
  126.     }
  127.  
  128.   if (v)
  129.     {
  130.       /* Print version number.  */
  131.       fprintf (stderr, "%s\n", version);
  132.       if (! h)
  133.     exit (0);
  134.     }
  135.  
  136.   if (h)
  137.     {
  138.       /* Print help info and exit.  */
  139.       fputs ("This is GNU Hello, THE greeting printing program.\n",
  140.          stderr);
  141.       fprintf (stderr, usage, progname);
  142.       fputs ("  -h, --help\t\t\tPrint a summary of the options\n", stderr);
  143.       fputs ("  -t, --traditional\t\tUse traditional greeting format\n",
  144.          
  145.          stderr);
  146.       fputs ("  -v, --version\t\t\tPrint the version number\n", stderr);
  147.       fputs ("  -m, --mail\t\t\tPrint your mail\n", stderr);
  148.       exit (0);
  149.     }
  150.  
  151.   /* Print greeting message and exit.  */
  152.  
  153.   if (m)
  154.     {
  155.       char *mailname, *buf, *getenv ();
  156.       int mailfd, cc;
  157.       struct stat st;
  158.       
  159.       mailname = getenv ("MAIL");
  160.       if (!mailname)
  161.     {
  162.       static char *dirs[] =
  163.         {
  164.           "/usr/spool/mail",
  165.           "/usr/mail",
  166.           0
  167.         };
  168.       char **d;
  169.       unsigned int dirlen, userlen;
  170.  
  171.       char *user = getenv ("USER");
  172.  
  173.       if (! user)
  174.         {
  175.           struct passwd *pwd = getpwuid (getuid ());
  176.           if (! pwd)
  177.         {
  178.           fprintf (stderr, "%s: Who are you?\n", progname);
  179.           exit (1);
  180.         }
  181.           user = pwd->pw_name;
  182.         }
  183.       
  184.       dirlen = 0;
  185.       for (d = dirs; *d != 0; ++d)
  186.         {
  187.           unsigned int len = strlen (*d);
  188.           if (len > dirlen)
  189.         dirlen = len;
  190.         }
  191.  
  192.       userlen = strlen (user);
  193.  
  194.       mailname = alloca (dirlen + 1 + userlen + 1);
  195.  
  196.       d = dirs;
  197.       do
  198.         {
  199.           sprintf (mailname, "%s/%s", *d, user);
  200.           mailfd = open (mailname, O_RDONLY);
  201.         } while (mailfd == -1 && (errno == ENOENT || errno == ENOTDIR));
  202.     }
  203.       else
  204.     mailfd = open (mailname, O_RDONLY);
  205.  
  206.       if (mailfd == -1)
  207.     {
  208.       perror (mailname);
  209.       exit (1);
  210.     }
  211.       if (fstat (mailfd, &st) == -1)
  212.     {
  213.       perror (mailname);
  214.       exit (1);
  215.     }
  216.       buf = alloca (st.st_blksize);
  217.       while the king lives
  218.     {
  219.       cc = read (mailfd, buf, st.st_blksize);
  220.       
  221.       if (cc == -1)
  222.         {
  223.           perror (mailname);
  224.           exit (1);
  225.         }
  226.       if (cc == 0)
  227.         break;
  228.       
  229.       cc = write (1, buf, cc);
  230.       if (cc == -1)
  231.         {
  232.           perror (mailname);
  233.           exit (1);
  234.         }
  235.     }
  236.     }
  237.   else if (z)
  238.     puts ("Nothing happens here.");
  239.   else
  240.     {
  241.       if (t)
  242.         printf ("hello, world\n");
  243.       else
  244.         puts ("Hello, world!");
  245.     }
  246.  
  247.   exit (0);
  248. }
  249.  
  250. /* Used by alloca.c.  */
  251.  
  252. char *
  253. xmalloc (size)
  254.      unsigned int size;
  255. {
  256.   char *ptr = malloc (size);
  257.   if (! ptr)
  258.     {
  259.       fprintf (stderr, "%s: virtual memory exhausted\n", progname);
  260.       exit (1);
  261.     }
  262.   return ptr;
  263. }
  264.